package com.edu.oracle.dao;

import java.sql.*;

public class LoginDao  {//user表
    public boolean login(String user, String password) {
        System.out.println("LoginDao实现访问数据，dao持久层"+user+password);

        /***
         * 作业1：书写登录的jdbc
         * 作业2：模仿登录功能，做student表的查询建类和书写方法
         */
        String url ="jdbc:oracle:thin:@//localhost:1521/orcl";
        String userOracle="scott";//数据库用户 userOracle
        String pass ="oracle";//数据库用户登录密码
        Connection connection = null;
        PreparedStatement pstm=null;
        ResultSet rs =null;

        try {
            //1.加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //2.创建连接对象
            connection= DriverManager.getConnection(url,userOracle,pass);

            //3.创建操作对象
            String sql="select * from student where sno=? and sclass=?";//查询语句
            pstm = connection.prepareStatement(sql);
            pstm.setString(1,user);
            pstm.setString(2,password);
            rs=pstm.executeQuery();//executeQuery连个单词拼接，第二个单词第一个字母要大写

            //4.处理结果集 +  >>> 返回结果
            if(rs.next()){
                return true;
            }else {
                return false;
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加载驱动出错");
        } catch (SQLException throwables) {
            System.out.println("创建连接对象");
            throwables.printStackTrace();
        } finally {//5.finally最终都会执行，关闭资源
            try {
                rs.close();
                pstm.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return false;
    }
}
